﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ProcessFile
{
 public class ConvertStk4StackupLayerRowTemplace
 {
     private string _row;
     private string _tempLace;
     private List<string> _subStackId;
     private int _lineIndex;
     public List<string[]> contentFileStk { set; get; }
     public bool IsTopLayer { set; get; }
     public bool IsBottomLayer { set; get; }
     public bool IsTopSolder { set; get; }
     public bool IsBottomSolder { set; get; }
     public string Unit { set; get; }
     public ConvertStk4StackupLayerRowTemplace(string row, string tempLace, int lineIndex)
     {
         _row = row;
         _tempLace = tempLace;
     
         _lineIndex = lineIndex;
     }
     public  void ReplaceName(string name)
     {
         ReplaceWithKeyWork("NAME", name);

     }
     public void ReplaceDielectricMaterial(string newValue)
     {
         ReplaceWithKeyWork("DIELMATERIAL", newValue);

     }
     public void ReplaceDielectricHeight(string newValue)
     {
         ReplaceWithKeyWork("DIELHEIGHT", newValue);

     }
     public void ReplaceDielectricCopThick(string newValue)
     {
         ReplaceWithKeyWork("COPTHICK", newValue);

     }
     public void ReplaceDielectricType(string newValue)
     {
         ReplaceWithKeyWork("DIELTYPE", newValue);

     }
     public void ReplaceDielectricConst(string newValue)
     {
         ReplaceWithKeyWork("DIELCONST", newValue);
     }
     public void ReplaceWithKeyWork(string key,string newValue)
     {
         string keyword = key+"=";
         int start = _tempLace.IndexOf(keyword);
         if (start > 0)
         {
             int endIndexOf = _tempLace.IndexOf(Common.SplipCharAD14, start);
             if (endIndexOf > start)
             {
                 string nameInFile = _tempLace.Substring(start, endIndexOf - start ).Trim();
                 string newText = key + "=" + newValue;
                 _tempLace = _tempLace.Replace(nameInFile, newText);
             }
         }

     }
     
     public string[] Item
     {
         get { return _row.Split(Common.SplipChar); }
     }
     public string LayerName
     {
         get
         {
             if (IsTopSolder)
             {
                 return "Top Solder";
             }
             if (IsBottomSolder)
             {
                 return "Bottom Solder";
             }
             string layerName = Item[Common.LayerNameIndex];
             return layerName;
         }
     }

     public override string ToString()
     {
         //replace name
         string layerName = LayerName;
         if (!string.IsNullOrEmpty(layerName))
         {
             ReplaceName(LayerName);
         }
        
         if (Common.CheckIsDielectric(_row))
         {
             int type;
             switch (Item[Common.DescriptionIndex])
             {
                 case Common.Core:
                     type = 1;
                     break;
                 case Common.Prepreg:
                     type = 2;
                     break;
                 default:
                     type = 3;
                     break;
             }
             ReplaceDielectricType(type.ToString());
             ReplaceDielectricConst(Item[Common.DielectricConstantIndex]);
             ReplaceDielectricHeight(Item[Common.DielectricThinknessIndex] + Unit);
             ReplaceDielectricMaterial(Item[Common.MaterialTypeIndex]);
         }
         else
         {
             if (Item[Common.DescriptionIndex] == Common.Plane)
             {
                 ReplaceDielectricCopThick(Item[Common.CopperThicknessIndex]+Unit);
             }
             ReplaceDielectricCopThick(Item[Common.CopperThicknessIndex] + Unit);
         }
         return _tempLace;
     }
    }
}
